﻿#include <iostream>
#include <vector>
#include <string>
#include <windows.h>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <algorithm>

using namespace std;

// 隐藏光标
void hideCursor() {
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO cursorInfo;
    GetConsoleCursorInfo(handle, &cursorInfo);
    cursorInfo.bVisible = false;
    SetConsoleCursorInfo(handle, &cursorInfo);
}

// 移动光标到指定位置
void gotoXY(int x, int y) {
    COORD coord = { (SHORT)x, (SHORT)y };
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

// 获取控制台窗口大小
void getConsoleSize(int& width, int& height) {
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
    width = csbi.srWindow.Right - csbi.srWindow.Left + 1;
    height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
}

// 生成颜色列表
vector<int> generateColors() {
    vector<int> colors;

    for (int i = 12; i <= 14; ++i) colors.push_back(i);

    for (int i = 10; i <= 11; ++i) colors.push_back(i);

    colors.push_back(9);
    colors.push_back(13);

    for (int i = 5; i <= 15; i += 2) colors.push_back(i);
    return colors;
}

int main() {
    srand(time(0));
    hideCursor();

    string name;
    cout << "请输入你的名字: ";
    getline(cin, name);

    vector<string> blessings = {
        " %s 要天天开心！",
        " %s 好运常伴！",
        " %s 心想事成！",
        " %s 前程似锦！",
        " %s 惊喜不断！",
        " %s 幸福安康！",
        " %s 万事顺意！",
        " %s 一飞冲天！",
        " %s 生活多彩！",
        " %s 勇攀高峰！",
        " %s 快乐每一天！",
        " %s 梦想成真！"
    };

    vector<int> colors = generateColors();
    int colorIndex = 0;

    int consoleWidth, consoleHeight;
    getConsoleSize(consoleWidth, consoleHeight);

    while (!_kbhit()) {
        int x = rand() % (consoleWidth - 20);
        int y = rand() % consoleHeight;

        char buffer[50];
        string msg = blessings[rand() % blessings.size()];
        // 修正snprintf的调用，格式化字符串
        snprintf(buffer, sizeof(buffer), msg.c_str(), name.c_str());
        string text(buffer);

        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors[colorIndex]);
        colorIndex = (colorIndex + 1) % colors.size();

        gotoXY(max(0, x), max(0, y));
        cout << text;

        Sleep(100);
    }

    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
    system("cls");
    cout << "感谢使用！祝福" << name << "一切顺利！" << endl;
    return 0;
}
